/*
 * Created on Jun 28, 2005
 *
 */
package com.erclab.internal.xpresso.webapp;

import java.util.ArrayList;
import java.util.Iterator;

import com.erclab.internal.xpresso.formelements.FormElement;
import com.erclab.internal.xpresso.forms.ClientFormValidator;
import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;

/**
 * @author Enrique Rodriguez.
 *
 */
public class JavascriptFormValidator implements ClientFormValidator{
	

	public JavascriptFormValidator(){
	}

	public void setClientScript(Form theForm) {
		StringBuffer required = new StringBuffer();
		required.append("function validate" + theForm.getIdForm() + "Form() { \nmessage=\"\";\n");
		required.append("theForm = document.Form" + theForm.getIdForm() + ";\n");
		
		ArrayList formElements = theForm.getFormElements();
		
		for (Iterator iter = formElements.iterator(); iter.hasNext();) {
			FormElement fe = (FormElement)iter.next();
			// Add validation if the field is required
			if(fe.getRequired()){
				required.append("if (theForm");
				required.append(".Field" + fe.getIdFormElement());
				required.append(".value ==" + '"' + '"' + " || theForm");
				required.append(".Field" + fe.getIdFormElement());
				required.append(".value == '-'){ \n" + "message = message + ");
				required.append('"' + "- Please provide \" + formatLabel('" + fe.getLabel() + "') + \"" + '\\' + 'n' + '"' + "\n" + "}\n");
			}
			if(fe.isModifiable() && !fe.isHidden()){
				String className = fe.getClassHandler().substring(fe.getClassHandler().lastIndexOf('.')+1, fe.getClassHandler().length());
				required.append("elementAnswer = " + className);
				required.append("Validate(theForm");
				required.append(".Field" + fe.getIdFormElement() + ", '" + fe.getLabel() + "');\n");
				required.append("if (elementAnswer != ''){\n");
				required.append("message = message + elementAnswer \n}\n");
			}
		}
		required.append("if ( message =='' ) {\ntheForm");
		required.append(".submit();\n}else{\nalert(message);\n}\n}");
				
		theForm.setValidationScript(required.toString());
		
	}
	
	public String getFilterValidationScript(Report theReport){
		StringBuffer script = new StringBuffer();
		script.append("function validate" + theReport.getReportName() + "Filters(){\nmessage=\"\";\n");
		script.append("theForm = document." + theReport.getReportName() + "Filters;\n");
		
		ArrayList filters = theReport.getReportFilters();
		if (filters != null){ 
			for (int i = 0; i < filters.size(); i++){
				ReportFilter f = (ReportFilter)filters.get(i);
				String className = f.getClassHandler().substring(f.getClassHandler().lastIndexOf('.')+1, f.getClassHandler().length());
				script.append("elementAnswer = " + className);
				script.append("Validate(document.getElementById('");
				script.append("Filter" + f.getColumnToFilter() + "'), '" + f.getLabel() + "');\n");
				script.append("if (elementAnswer != ''){\n");
				script.append("message = message + elementAnswer \n}\n");
				
			}
		}
		
		script.append("if ( message =='' ) {\ndocument." + theReport.getReportName() + "Filters");
		script.append(".submit();\n}else{\nalert(message);\n}\n}");
		return script.toString();
	}

}
